package com.sservice.base.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by suweiming on 2017/12/20.
 */

public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
    public static Object mergeTo(Object dest, Object source) {
        return mergeTo(dest, source, true);
    }

    public static Object mergeTo(Object dest, Object source, boolean sourceIsParent) {
//        source.getClass();
        if (sourceIsParent) {
            if (!dest.getClass().getName().equals(source.getClass().getName())) {
                throw new Error("object class not the same !");
            }
        }

        Class[] argsClass = new Class[0];

        Method[] methods = source.getClass().getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            if (m.getName().indexOf("get") > -1) {
                try {
                    Method method = dest.getClass().getMethod(m.getName().replace("get", "set"), m.getReturnType());
                    Object o = m.invoke(source, argsClass);
                    if (o != null) {
                        method.invoke(dest, o);
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
//        methods.
        return dest;

    }
}
